﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MSlider
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();
       
        public List<Slider> FindAll()
        {
            var v = from f in context.Sliders orderby f.ThuTu ascending select f;
            return v.ToList();
        }
        public List<Slider> FindAll_Web()
        {
            var v = from f in context.Sliders.Where(d => d.HienThi == true) orderby f.ThuTu descending select f;
            return v.ToList();
        }
        public int GetTotalSlider()
        {
            return FindAll().Count();
        }
        public int GetTotalSlider_Web()
        {
            return FindAll_Web().Count();
        }
        public void Save(Slider onl)
        {
            context.Sliders.InsertOnSubmit(onl);
            context.SubmitChanges();
        }
        public void Update(Slider onl)
        {
            var v = from f in context.Sliders
                    select f;
            Slider onldb = v.SingleOrDefault(d => d.MaSlider.Equals(onl.MaSlider));
            onldb.TenHinh = onl.TenHinh;         
            onldb.ThuTu = onl.ThuTu;
            context.SubmitChanges();
        }
        public void SaveChange()
        {
        }
        public void HienThiSldier(int id)
        {
            var v = from f in context.Sliders
                    where f.MaSlider == id
                    select f;
            Slider spdb = v.SingleOrDefault(d => d.MaSlider == id);
            if (spdb.HienThi == true)
            {
                spdb.HienThi = false;
            }
            else spdb.HienThi = true;
            context.SubmitChanges();
        }
        public void Delete(int id)
        {
            var v = from p in context.Sliders
                    where p.MaSlider.Equals(id)
                    select p;
            Slider onldb = v.SingleOrDefault();
            context.Sliders.DeleteOnSubmit(onldb);
            context.SubmitChanges();
        }
        public Slider GetByID(int id)
        {
            return (FindAll().Where(d => d.MaSlider == id)).SingleOrDefault();
        }
        public int GetMax()
        {
            return (from t in context.Sliders
                    orderby t.MaSlider descending
                    select t.MaSlider).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.Sliders
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
    }
}